package adapter

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"migration-tool/target"
	"sync"
	"testing"
	"time"
)

func TestAoneGetAllExportRepoInfo(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repos, err := aoneGetAllRepoInfoForExport(cfg)
	assert.Equal(t, err, nil)
	fmt.Println(len(repos))
	//for _, val := range repos {
	//fmt.Println(val)
	//}
}

func TestAoneGetAllImportRepoInfo(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repos, err := aoneGetAllRepoInfoForImport(cfg)
	assert.Equal(t, err, nil)
	fmt.Println(len(repos))
	//for _, val := range repos {
	//fmt.Println(val)
	//}
}

func TestAoneGetAllGroup(t *testing.T) {
	cfg := getUseProgressConfig(t)
	args := TaskArgs{
		TaskName:       GlobalTaskNameGroup,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            cfg.GlobalLog,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err := aoneDealGroup(args)
	assert.Equal(t, err, nil)

	var groupList target.CodeupNamespaceList
	dataArgs := DataArgs{
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameGroups,
		ExportPlatformName: AonePlatformName,
		EnterpriseName:     cfg.BaseConfig.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &groupList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)
	fmt.Println(len(groupList.NamespaceList))

	for _, val := range groupList.NamespaceList {
		fmt.Println(val.String())
	}
	var memberList target.CodeupMemberList
	dataArgs = DataArgs{
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameGroupMembers,
		ExportPlatformName: AonePlatformName,
		EnterpriseName:     cfg.BaseConfig.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &memberList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(memberList.RepoMember))
	for _, val := range memberList.RepoMember {
		fmt.Println(val.String())
	}
}

func TestAoneDealDeployKeySetting(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameDeployKeysSetting,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealDeployKeySetting(args)
	assert.Equal(t, err, nil)

	var data target.CodeupDeployKeysSettingList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameDeployKeysSetting,
		ExportPlatformName: AonePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	for _, val := range data.DeployKeysList {
		fmt.Println(val.String())
	}
}

func TestAoneDealWebHookSetting(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameWebHookSetting,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealWebHookSetting(args)
	assert.Equal(t, err, nil)

	var data target.CodeupWebHookSettingList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameWebHookSetting,
		ExportPlatformName: AonePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	for _, val := range data.WebHookList {
		fmt.Println(val.String())
	}
}

func TestAoneDealPushRuleSetting(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNamePushRuleSetting,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealPushRuleSetting(args)
	assert.Equal(t, err, nil)

	var data target.CodeupPushRuleSettingList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNamePushRuleSetting,
		ExportPlatformName: AonePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	for _, val := range data.PushRuleSettingList {
		fmt.Println(val.String())
	}
}

func TestAoneDealCodeReviewSetting(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameCodeReviewSetting,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealCodeReviewSetting(args)
	assert.Equal(t, err, nil)

	var data target.CodeupCodeReviewSetting
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameCodeReviewSetting,
		ExportPlatformName: AonePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)
	fmt.Println(data.String())
}

func TestAoneDealProtectBranchSetting(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameProtectBranchSetting,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealPushRuleSetting(args)
	assert.Equal(t, err, nil)

	var data target.CodeupProtectBranchSettingList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameProtectBranchSetting,
		ExportPlatformName: AonePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	for _, val := range data.ProtectBranchSettingList {
		fmt.Println(val.String())
	}
}

func TestAoneDealRepoMetaInfo(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameRepoMetaInfo,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealRepoMetaInfo(args)
	assert.Equal(t, err, nil)

	var data target.CodeupRepoMetaInfo
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameRepoMetaInfo,
		ExportPlatformName: AonePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)
	fmt.Println(data.String())
}

func TestAoneDealRepoMember(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameRepoMember,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealRepoMember(args)
	assert.Equal(t, err, nil)

	var data target.CodeupMemberList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameRepoMembers,
		ExportPlatformName: AonePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)
	for _, val := range data.RepoMember {
		fmt.Println(val.String())
	}
}

func TestAoneDealCode(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameCode,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealCode(args)
	assert.Equal(t, err, nil)
}

func TestAoneDealWiki(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameWiki,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealWiki(args)
	assert.Equal(t, err, nil)
}

func TestAoneDealMergeRequest(t *testing.T) {
	cfg := getUseProgressConfig(t)

	//repoInfo := RepoInfo{
	//	RepoId:           2754725,
	//	RepoGroupName:    "agit",
	//	RepoName:         "test_migration",
	//	CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
	//	WikiEnable:       true,
	//}

	repoInfo := RepoInfo{
		RepoId:           1549172,
		RepoGroupName:    "agit",
		RepoName:         "satellite",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/satellite.git",
		WikiEnable:       true,
	}
	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)
	args := TaskArgs{
		TaskName:       TaskNameMergeRequest,
		RepoInfo:       &repoInfo,
		ExportDataDir:  cfg.BaseConfig.ExportDataDir,
		Log:            log,
		Token:          cfg.BaseConfig.Token,
		EnterpriseName: cfg.BaseConfig.EnterpriseName,
		PageSize:       cfg.BaseConfig.PageSize,
	}

	err = aoneDealMergeRequest(args)
	assert.Equal(t, err, nil)

	var data target.CodeupMergeRequestList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameMergeRequests,
		ExportPlatformName: AonePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)
	for _, val := range data.MergeRequestList {
		fmt.Println(val.String())
	}
}

func TestTime(t *testing.T) {
	str := time.Now()
	loc, _ := time.LoadLocation("Asia/Shanghai")
	tt, _ := time.ParseInLocation(time.DateTime, str.Format(time.DateTime), loc)

	fmt.Println(str)
	fmt.Println(tt.Format(time.DateTime))
}

// Aone的Merge单元测试——User
func TestAoneMergeAllUsers(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	err := aoneMergeAllUsers(cfg)
	assert.Equal(t, err, nil)

	var userList target.CodeupUserList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		FileName:           BinFileNameAllUsersProto,
		EnterpriseName:     cfg.BaseConfig.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &userList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(userList.UserList))
	for _, val := range userList.UserList {
		fmt.Println(val)
	}
}

func TestAoneMergeOneRepoUsers(t *testing.T) {
	cfg := getUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:           2754725,
		RepoGroupName:    "agit",
		RepoName:         "test_migration",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)

	userMap := sync.Map{}

	args := MergeArgs{
		Repo:       &repoInfo,
		UserMap:    &userMap,
		BaseConfig: &cfg.BaseConfig,
		Log:        log,
	}

	err = aoneMergeUsersByRepo(args)
	assert.Equal(t, err, nil)

	userMap.Range(func(key, value any) bool {
		user := value.(*target.CodeupUser)
		fmt.Println(user.String())
		return true
	})
}

// Aone的Merge单元测试——MergeRequest
func TestAoneMergeAllMergeRequests(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	err := aoneMergeAllMergeRequests(cfg)
	assert.Equal(t, err, nil)

	repoInfo := RepoInfo{
		RepoId:           1549172,
		RepoGroupName:    "agit",
		RepoName:         "satellite",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/satellite.git",
		WikiEnable:       true,
	}

	var mrList target.CodeupMergeRequestList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		FileName:           BinFileNameMergeRequestsMerge,
		FilePathTmpl:       ExportDataFileTmpl,
		RepoInfo:           &repoInfo,
		Data:               &mrList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(mrList.MergeRequestList))
	for _, val := range mrList.MergeRequestList {
		fmt.Println(val.Id, val.Title, time.Unix(val.UpdateAt, 0), len(val.CommentList))
	}
}

func TestAoneMergeOneRepoMergeRequests(t *testing.T) {
	cfg := getUseProgressConfig(t)

	//repoInfo := RepoInfo{
	//	RepoId:           2754725,
	//	RepoGroupName:    "agit",
	//	RepoName:         "test_migration",
	//	CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/test_migration.git",
	//	WikiEnable:       true,
	//}
	repoInfo := RepoInfo{
		RepoId:           1549172,
		RepoGroupName:    "agit",
		RepoName:         "satellite",
		CodeCloneHttpUrl: "http://gitlab.alibaba-inc.com/agit/satellite.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, AonePlatformName)
	assert.Equal(t, err, nil)

	args := MergeArgs{
		Repo:       &repoInfo,
		UserMap:    nil,
		BaseConfig: &cfg.BaseConfig,
		Log:        log,
	}

	err = aoneMergeMergeRequestsByRepo(args)
	assert.Equal(t, err, nil)

	var mrList target.CodeupMergeRequestList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: AonePlatformName,
		FileName:           BinFileNameMergeRequestsMerge,
		FilePathTmpl:       ExportDataFileTmpl,
		RepoInfo:           &repoInfo,
		Data:               &mrList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(mrList.MergeRequestList))
}
